import { useEffect, useState } from 'react';
import { useRequest, defaultFormatResult } from '@/hooks';
import { queryCandidateProcess } from '@/pages/EntryManage/services';
import { replaceToWaitEntry } from '@/pages/EntryManage/utils';

/**
 * 判断是否为当前环节
 * @param {String} entryPersonnelCode 入职人员唯一标识
 * @param {Number} processStatus 当前入职环节
 */
const useCandidateProcess = (entryPersonnelCode, processStatus, employeeCode) => {
    const [isCurrentProcess, setIsCurrentProcess] = useState(false);
    const { data } = useRequest(() => queryCandidateProcess(entryPersonnelCode), {
        ...defaultFormatResult
    });
    const { entryStatus, assignEmployeeList } = data || {};

    useEffect(() => {
        if (entryStatus) {
            if (employeeCode) {
                const result = assignEmployeeList.filter(item => item.employeeCode === employeeCode);
                if (!result.length) {
                    replaceToWaitEntry('您不是当前节点处理人');
                }
            }

            if (processStatus) {
                setIsCurrentProcess(entryStatus === processStatus);
            }
        }
    }, [employeeCode, entryStatus, assignEmployeeList, processStatus]);

    return isCurrentProcess;
};

export default useCandidateProcess;
